package com.saifei.people.controller;


import com.saifei.common.R;
import com.saifei.people.entity.SaifeiAdmin;
import com.saifei.people.entity.vo.LoginParamVo;
import com.saifei.people.service.SaifeiAdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class LoginController {

    @Autowired
    SaifeiAdminService saifeiAdminService;

    @PostMapping("login")
    public R login(@RequestBody LoginParamVo vo) {
        String token = saifeiAdminService.login(vo);
        if(token != null)
            return R.ok().data("token", token);
        else return R.error();
    }

    @GetMapping("info")
    public R info(@RequestParam("token") String token) {
        SaifeiAdmin userInfo = saifeiAdminService.getInfo(token);
        if(userInfo != null)
            return R.ok().data("roles", "[管理员]").data("avatar", userInfo.getHeader()).data("name", userInfo.getUsername());
        else return R.error();
    }

}
